package com.wskeee.ec.components.skin
{
	import com.wskeee.ec.ens;
	import com.wskeee.ec.components.Button;
	import com.wskeee.ec.components.Image;
	import com.wskeee.ec.components.Label;
	import com.wskeee.ec.containers.LayoutConst;
	import com.wskeee.ec.core.EComponent;
	import com.wskeee.ec.styles.Icons;
	import com.wskeee.ec.styles.StyleNames;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;

	public class RadioButtonSkin extends BaseSkinDrawer
	{
		protected var icon:Image;
		protected var selectedIcon:Image;
		protected var label:TextField
		
		use namespace ens;
		
		//========================= init ========================
		override public function init(target:EComponent, ui:*):void
		{
			super.init(target,ui);
			initSkin();
		}
		protected function initSkin():void
		{
			icon = new Image();
			selectedIcon = new Image();
			icon.addEventListener(Event.RESIZE,iconResizeHandler);
			selectedIcon.addEventListener(Event.RESIZE,iconResizeHandler);
			clip.addChild(icon);
			clip.addChild(selectedIcon);
			label = new TextField();
			label.autoSize = TextFieldAutoSize.LEFT;
			clip.addChild(label);
		}
		
		private function iconResizeHandler(event:Event):void
		{
			validateDisplayList();
		}
		
		override public function destory():void
		{
			icon.removeEventListener(Event.RESIZE,iconResizeHandler);
			selectedIcon.removeEventListener(Event.RESIZE,iconResizeHandler);
			super.destory();
		}
		//========================= end =========================
		
		//========================= redraw ======================
		override public function redraw():void
		{
			var skin:Object = getSkin("upIcon");
			if(!skin)skin = getUpIcon();
			icon.source = skin;
			
			skin = getSkin("selectedIcon");
			if(!skin)skin = getSelectedIcon();
			selectedIcon.source = skin;
		}
		
		protected function getUpIcon():Object
		{
			return Icons.radioButtonIcon;
		}
		
		protected function getSelectedIcon():Object
		{
			return Icons.radioButtonSelectedIcon;
		}
		
		override public function validateProperties():void
		{
			super.validateProperties();
			icon.visible = !Button(target).selected;
			selectedIcon.visible =  Button(target).selected;
			label.text = Button(target).label;
			
		}
		override public function validateDisplayList():void
		{
			super.validateDisplayList();
			var currentIcon:Image = Button(target).selected ? selectedIcon : icon;
			if(btn.labelPlacement == LayoutConst.RIGHT)
			{
				label.x = currentIcon.width + getStyle("horizontalGap") as Number;
			}else
			{
				currentIcon.x = label.width + getStyle("horizontalGap") as Number;
			}
			
			target.width = currentIcon.width + label.width + getStyle("horizontalGap");
			target.height = label.height;
			
			currentIcon.y = (target.height - currentIcon.height)/2;
		}
		//========================= end =========================
		
		//========================= get & set ===================
		protected function get clip():Sprite
		{
			return ui as Sprite;
		}
		protected function get btn():Button
		{
			return target as Button;
		}
		//========================= end =========================
	}
}